package com.example.doctor.mapper;

import com.example.doctor.dto.req.SearchXDiseaseReq;
import com.example.doctor.dto.req.SearchXDrugReq;
import com.example.doctor.dto.req.SearchZDrugReq;
import com.example.doctor.dto.resp.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

@Component
public interface TempleteMapper {
    @Select("SELECT " +
            "a.drug_id_bigint AS DrugID ,b.drug_name_nvarchar AS DrugName,a.num_int AS Num,a.guild_bit AS Guild,a.tid_bigint AS TID " +
            "FROM z_drug_template_drug a " +
            "INNER JOIN z_drug_base_info b " +
            "ON(a.drug_id_bigint=b.drug_id_bigint) " +
            "WHERE b.disable_bit= 0 " +
            "AND a.tid_bigint=#{tId}")
    List<TDrug> queryByTid(@Param("tId") Long tId);

    

    @Select("SELECT tid_bigint,t_name_nvarchar,1 as ttype,create_time_datetime FROM z_drug_template WHERE dcid_bigint = #{dcid}"
    +" union all"
    +" select t_id_bigint as tid_bigint,t_name_nvarchar,2 as ttype,create_time_datetime from x_drug_templete where dc_id_bigint = #{dcid} and d_type_tinyint = 3"
    +" order by create_time_datetime desc")
    List<Map<String, Object>> selectZTemplate(@Param("dcid") Long dcid);

    @Select("SELECT " +
            "a.drug_id_bigint AS DrugID,b.drug_name_nvarchar AS DrugName,b.spec_nvarchar AS Spec,a.num_int AS Num, a.t_id_bigint AS TID " +
            "FROM x_drug_templete_drug a " +
            "INNER JOIN x_drug_base_info b " +
            "ON (a.drug_id_bigint=b.drug_id_bigint) " +
            "WHERE b.disable_bit=0 " +
            "AND a.t_id_bigint=#{tId}")
    List<TDrug> queryByTidx(@Param("tId") Long tId);

    @SelectProvider(type = TempleteMapper.CommonProvider.class, method = "SearchZDrug")
    List<SearchZDrugRsp> SearchZDrug(@org.springframework.data.repository.query.Param("searchZDrugReq") SearchZDrugReq searchZDrugReq);

    @Select("SELECT " +
            "a.drug_id_bigint AS DrugID,b.drug_name_nvarchar AS DrugName,a.num_int AS Num,a.guild_bit AS Guild " +
            "FROM z_drug_template_drug a " +
            "INNER JOIN z_drug_base_info b " +
            "ON(a.drug_id_bigint=b.drug_id_bigint) " +
            "WHERE b.disable_bit=FALSE AND a.tid_bigint =#{tId}")
    List<ZInfoDrugRsp> ZInfo(@Param("tId") Long tId);

    @SelectProvider(type = TempleteMapper.CommonProvider.class, method = "SearchXDisease")
    List<SearchXDiseaseRsp> SearchXDisease(@org.springframework.data.repository.query.Param("searchXDiseaseReq") SearchXDiseaseReq searchXDiseaseReq);

    @SelectProvider(type = TempleteMapper.CommonProvider.class, method = "SearchXDrug")
    List<SearchXDrugRsp> SearchXDrug(@org.springframework.data.repository.query.Param("searchXDrugReq") SearchXDrugReq searchXDrugReq);

    @Select("SELECT a.drug_id_bigint AS DrugID,b.drug_name_nvarchar AS DrugName,b.spec_nvarchar AS Spec,b.unit_nvarchar AS Unit,b.does_unit_nvarchar " +
            "AS DoesUnit,b.img_nvarchar AS Img,a.num_int AS Num,a.med_way_nvarchar " +
            "AS MedWay,a.freq_nvarchar AS Freq,a.does_int AS Does,a.day_int " +
            "AS Day,a.remark_nvarchar AS Remark FROM x_drug_templete_drug a " +
            "INNER JOIN x_drug_base_info b ON(a.drug_id_bigint=b.drug_id_bigint) WHERE b.disable_bit=FALSE AND a.t_id_bigint=#{tId}")
    List<TDrugx> XInfo(@Param("tId") Long tId);


    class CommonProvider extends BaseProvider {
        public String SearchZDrug(@Param("searchZDrugReq") SearchZDrugReq searchZDrugReq) {
            SQL sql = new SQL();
            sql.SELECT("a.drug_id_bigint AS DrugID,drug_name_nvarchar AS DrugName FROM z_drug_base_info a ");
            sql.WHERE("a.disable_bit= 0");
            if (!ObjectUtils.isEmpty(searchZDrugReq.getDids())) {
                String[] split = searchZDrugReq.getDids().split(",");
                filterFieldNotId(sql, "a.drug_id_bigint", split);
            }
            if (!ObjectUtils.isEmpty(searchZDrugReq.getPy())) {
                String pay = searchZDrugReq.getPy();
                sql.WHERE("(a.py_nvarchar LIKE CONCAT ('%',#{py},'%') or a.drug_name_nvarchar LIKE CONCAT ('%',#{py},'%'))");
            }
            return sql.toString();
        }

        public String SearchXDisease(SearchXDiseaseReq searchXDiseaseReq) {
            SQL sql = new SQL();
            sql.SELECT(" a.id_bigint AS ID,a.code_nvarchar as code,a.disease_name_nvarchar AS DiseaseName FROM h_disease_info a ");
            sql.WHERE(" a.disable_bit = 0");
            int disType = searchXDiseaseReq.getDisType();
            sql.WHERE(" a.d_type_int = #{disType}");
            if (!ObjectUtils.isEmpty(searchXDiseaseReq.getDis())) {
                String dis = searchXDiseaseReq.getDis();
                sql.WHERE("(a.code_nvarchar LIKE CONCAT ('%',#{dis},'%') or a.disease_name_nvarchar LIKE CONCAT ('%',#{dis},'%'))");
            }
            return sql.toString() + " limit 50";
        }

        public String SearchXDrug(SearchXDrugReq searchXDrugReq) {
            SQL sql = new SQL();
            sql.SELECT("a.drug_id_bigint AS DrugID,a.drug_name_nvarchar AS DrugName,a.spec_nvarchar AS " +
                    "Spec,a.img_nvarchar AS Img,a.does_unit_nvarchar AS DoesUnit,a.unit_nvarchar AS Unit ,a.med_way_nvarchar AS MedWay ,a.does_int AS Does,a.freq_nvarchar AS Freq FROM x_drug_base_info a");
            sql.WHERE(" a.disable_bit= 0");

            Byte dType = searchXDrugReq.getDType();
            sql.WHERE(" a.drug_type_tinyint = #{dType}");
            if (!ObjectUtils.isEmpty(searchXDrugReq.getDids())) {
                String[] split = searchXDrugReq.getDids().split(",");
                filterFieldNotId(sql, "a.drug_id_bigint", split);
            }
            if (!ObjectUtils.isEmpty(searchXDrugReq.getPy())) {
                String pay = searchXDrugReq.getPy();
                sql.WHERE("(a.py_nvarchar LIKE CONCAT ('%',#{py},'%') or a.drug_name_nvarchar LIKE CONCAT ('%',#{py},'%'))");
            }

            return sql.toString();
        }
    }
}
